package com.hiep.connect.client.presenter;

import java.util.List;

import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ContextMenuEvent;
import com.google.gwt.event.dom.client.ContextMenuHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;
import com.hiep.connect.client.AppController;
import com.hiep.connect.client.MyCommand;
import com.hiep.connect.client.event.AcceptFriendEvent;
import com.hiep.connect.client.event.PostOnFriendEvent;
import com.hiep.connect.client.event.UserLogoutEvent;
import com.hiep.connect.client.event.UserSearchEvent;
import com.hiep.connect.client.event.UserUpdateEvent;
import com.hiep.connect.client.view.ContactsView;
import com.hiep.connect.client.view.EditProfileView;
import com.hiep.connect.client.view.HomeView;
import com.hiep.connect.client.view.LoginView;
import com.hiep.connect.shared.MyUser;

public class HomeViewPresenter implements Presenter {

	public interface HomeDisplay {
		Widget asWidget();

		void setSearchHandler(KeyUpHandler handler);

		void setLogoutCommand(Command logoutCommand);

		void setAcceptFriendCommand(Command acceptCommand);

		void setModel(MyUser user);

		Tree getTree();

		Label getStatus();

		SuggestBox getSuggestBox();

		ScrollPanel getContentScrollPanel();
	}

	private MyUser user;
	private HandlerManager eventBus;
	private HomeDisplay view;
	private PopupPanel popupPanel;
	private TextBox postField;
	private Tree friendTree;
	private static boolean ignoreEvent = false;

	public HomeViewPresenter(MyUser user, HandlerManager eventBus,
			HomeDisplay view) {

		this.user = user;
		this.eventBus = eventBus;
		this.view = view;
		initPopupPanel();

		bind();
	}

	public void bind() {
		view.setModel(user);

		Command acceptCommand = new Command() {

			@Override
			public void execute() {
				// TODO Auto-generated method stub
				eventBus.fireEvent(new AcceptFriendEvent(user));
			}
		};
		view.setAcceptFriendCommand(acceptCommand);

		Command logoutCommand = new Command() {

			@Override
			public void execute() {
				eventBus.fireEvent(new UserLogoutEvent(user));
			}
		};
		view.setLogoutCommand(logoutCommand);

		KeyUpHandler keyUpHandler = new KeyUpHandler() {

			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (ignoreEvent) {
					return;
				}

				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					ignoreEvent = true;
					String searchFor = view.getSuggestBox().getText().trim();
					view.getStatus().setText("Searching for " + searchFor);
					eventBus.fireEvent(new UserSearchEvent(searchFor));
					ignoreEvent = false;
				}
			}
		};
		view.setSearchHandler(keyUpHandler);

		createFriendTree();
	}

	private void initPopupPanel() {
		HorizontalPanel panel = getPostPanel();
		popupPanel = new PopupPanel(true);
		popupPanel.add(panel);

	}

	private HorizontalPanel getPostPanel() {
		postField = new TextBox();
		postField.addKeyUpHandler(new KeyUpHandler() {

			@Override
			public void onKeyUp(KeyUpEvent event) {
				// TODO Auto-generated method stub
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					postOnFriend();
				}

			}
		});

		Button postButton = new Button("Post");
		postButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				postOnFriend();
			}
		});
		HorizontalPanel panel = new HorizontalPanel();
		panel.setStylePrimaryName("Hiep-Popup");
		panel.setSpacing(5);
		panel.add(postField);
		panel.add(postButton);
		return panel;
	}

	private void postOnFriend() {
		popupPanel.hide();
		String message = postField.getText().trim();
		TreeItem selectedItem = friendTree.getSelectedItem();
		String email = selectedItem.getText().trim();
		view.getStatus().setText("Message has been posted on " + email);
		MyUser target = new MyUser(email);

		eventBus.fireEvent(new PostOnFriendEvent(user, target, message));
	}

	private void createFriendTree() {
		friendTree = view.getTree();

		((Widget) friendTree).addDomHandler(new ContextMenuHandler() {

			@Override
			public void onContextMenu(ContextMenuEvent event) {
				// TODO Auto-generated method stub
				event.preventDefault();
				event.stopPropagation();

			}
		}, ContextMenuEvent.getType());

		friendTree.addMouseDownHandler(new MouseDownHandler() {

			@Override
			public void onMouseDown(MouseDownEvent event) {

				if (event.getNativeButton() == NativeEvent.BUTTON_RIGHT) {

					TreeItem selectedItem = friendTree.getSelectedItem();
					if (selectedItem != null) {
						String msg = "right lick on " + selectedItem.getText()
								+ " detected";
						Label msgLabel = new Label("right lick on "
								+ selectedItem.getText() + " detected");
						// Window.alert(msg);
						Widget source = (Widget) event.getSource();
						int left = source.getAbsoluteLeft();
						int top = source.getAbsoluteTop();
						popupPanel.setPopupPosition(left, top);
						popupPanel.showRelativeTo(selectedItem);
						postField.setText("");
						postField.setFocus(true);

						event.preventDefault();
						event.stopPropagation();

					}
				}
			}
		});
	}

	public void go(final HasWidgets container) {
		container.clear();
		container.add(view.asWidget());
	}

}
